<template>
  <div class="position-container">
    <FeatureHeader img="https://cdn-icons-png.flaticon.com/512/2920/2920256.png" title="职位管理" />
    <div class="toolbar">
      <el-button type="primary" @click="handleAdd">新增职位</el-button>
    </div>

    <el-table :data="positionList" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="name" label="职位名称"></el-table-column>
      <el-table-column prop="department" label="所属部门"></el-table-column>
      <el-table-column prop="level" label="职级"></el-table-column>
      <el-table-column prop="baseSalary" label="基本工资"></el-table-column>
      <el-table-column prop="description" label="职位描述"></el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增/编辑对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
      <BaseForm :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="职位名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="所属部门" prop="department">
          <el-select v-model="form.department" placeholder="请选择部门">
            <el-option
              v-for="item in departments"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职级" prop="level">
          <el-select v-model="form.level" placeholder="请选择职级">
            <el-option label="P1" value="P1"></el-option>
            <el-option label="P2" value="P2"></el-option>
            <el-option label="P3" value="P3"></el-option>
            <el-option label="P4" value="P4"></el-option>
            <el-option label="P5" value="P5"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="基本工资" prop="baseSalary">
          <el-input-number v-model="form.baseSalary" :min="0" :step="1000"></el-input-number>
        </el-form-item>
        <el-form-item label="职位描述" prop="description">
          <el-input type="textarea" v-model="form.description"></el-input>
        </el-form-item>
      </BaseForm>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import FeatureHeader from '@/components/FeatureHeader.vue'
import BaseForm from '@/components/BaseForm.vue'
import { mapState, mapActions } from 'vuex'

export default {
  name: 'PositionList',
  components: { FeatureHeader, BaseForm },
  data() {
    return {
      dialogVisible: false,
      dialogTitle: '新增职位',
      form: {
        name: '',
        department: '',
        level: '',
        baseSalary: 0,
        description: ''
      },
      rules: {
        name: [{ required: true, message: '请输入职位名称', trigger: 'blur' }],
        department: [{ required: true, message: '请选择所属部门', trigger: 'change' }],
        level: [{ required: true, message: '请选择职级', trigger: 'change' }],
        baseSalary: [{ required: true, message: '请输入基本工资', trigger: 'change' }],
        description: [{ required: true, message: '请输入职位描述', trigger: 'blur' }]
      },
      departments: [
        { value: '技术部', label: '技术部' },
        { value: '人事部', label: '人事部' },
        { value: '财务部', label: '财务部' },
        { value: '市场部', label: '市场部' }
      ],
      editMode: false
    }
  },
  computed: {
    ...mapState({
      positionList: state => state.position.list
    })
  },
  created() {
    this.fetchList();
  },
  methods: {
    ...mapActions({
      fetchList: 'position/fetchList',
      addPosition: 'position/addPosition',
      editPosition: 'position/editPosition',
      deletePosition: 'position/deletePosition'
    }),
    handleAdd() {
      this.dialogTitle = '新增职位';
      this.editMode = false;
      if (this.$refs.form) {
        this.$refs.form.resetFields();
      }
      this.form = {
        name: '',
        department: '',
        level: '',
        baseSalary: 0,
        description: ''
      };
      this.dialogVisible = true;
    },
    handleEdit(row) {
      this.dialogTitle = '编辑职位';
      this.editMode = true;
      this.form = { ...row };
      this.dialogVisible = true;
    },
    handleDelete(row) {
      this.$confirm('确认删除该职位?', '提示', {
        type: 'warning'
      }).then(() => {
        this.deletePosition(row.id);
        this.$message.success('删除成功');
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.editMode) {
            this.editPosition({ ...this.form });
            this.$message.success('编辑成功');
          } else {
            this.addPosition({ ...this.form });
            this.$message.success('新增成功');
          }
          this.dialogVisible = false;
        } else {
          this.$message.error('表单验证失败，请检查输入项');
        }
      });
    }
  }
}
</script>

<style scoped>
.position-container {
  padding: 20px;
}

.toolbar {
  margin-bottom: 20px;
}
</style> 